package com.tzy.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FastjsonUtil {

    private static final SerializeConfig config;

    static {
        config = new SerializeConfig();
        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
    }

    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null，输出为[]，而不是null
            SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null，输出为0，而不是null
            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null，输出为false，而不是null
            SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null，输出为""，而不是null
    };
    /**
     * 将对象转换为json
     * null ->list =[]
     * 输出null 值
     * @param value 对象
     * @return
     */
    public static String obj2json(Object value) {
        try {
            return JSON.toJSONString(value,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty);
        } catch (Exception e) {
            try {
                return obj2json3(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String obj2jsonPretty(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.PrettyFormat);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String obj2jsonDateFormat(Object value) {
        try {
            return JSON.toJSONStringWithDateFormat(value, "yyyy-MM-dd HH:mm:ss",
                    SerializerFeature.WriteDateUseDateFormat,SerializerFeature.DisableCircularReferenceDetect);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String obj2jsonDateFormat(Object value, String format) {
        try {
//			return JSON.toJSONString(value, SerializerFeature.PrettyFormat);
            return JSON.toJSONStringWithDateFormat(value, format,
                    SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    @SuppressWarnings("deprecation")
    public static String obj2json2(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.WriteTabAsSpecial, SerializerFeature.WriteSlashAsSpecial);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressWarnings("deprecation")
    public static String obj2json3(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.WriteTabAsSpecial, SerializerFeature.WriteSlashAsSpecial,
                    SerializerFeature.BrowserCompatible);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json转换为对象
     *
     * @param <T>   转换的类型
     * @param json  json数据
     * @param clazz 被转化对象的class
     * @return
     */
    public static <T> T json2obj(String json, Class<T> clazz) {
        try {
            return JSON.parseObject(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json转换为对象数组
     *
     * @param <T>   转换的类型
     * @param json  json数据
     * @param clazz 被转化对象的class
     * @return
     */
    public static <T> List<T> json2List(String json, Class<T> clazz) {
        try {
            return JSON.parseArray(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * jsonStr to Map
     *
     * @param jsonStr jsonStr
     * @return
     * @author tongys
     * @date 2013-4-2
     */
    public static Map<String, String> parseJSON2Map(String jsonStr) {

        // 参数di
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json = null;

        // 最外层解析
        try {
            json = JSONObject.parseObject(jsonStr);
            for (String k : json.keySet()) {
                Object v = json.get(k);
                if (null != v) {
                    map.put(k.toString(), v.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }

    /**
     * jsonStr to Map
     *
     * @param jsonStr jsonStr
     * @return
     */
    public static Map<String, Object> parseJSON2Map2(String jsonStr) {

        // 参数di
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject json = null;

        // 最外层解析
        try {
            json = JSONObject.parseObject(jsonStr);
            for (String k : json.keySet()) {
                Object v = json.get(k);
                if (null != v) {
                    map.put(k.toString(), v.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }
//https://blog.csdn.net/wangnanwlw/article/details/112307036

    public static String convertObjectToJSON(Object object) {
        return JSON.toJSONString(object, config, features);
    }

    public static String toJSONNoFeatures(Object object) {
        return JSON.toJSONString(object, config);
    }


    public static Object toBean(String text) {
        return JSON.parse(text);
    }

    public static <T> T toBean(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    /**
     * 转换为数组
     *
     * @param text
     * @return
     */
    public static <T> Object[] toArray(String text) {
        return toArray(text, null);
    }

    /**
     * 转换为数组
     *
     * @param text
     * @param clazz
     * @return
     */
    public static <T> Object[] toArray(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz).toArray();
    }

    /**
     * 转换为List
     *
     * @param text
     * @param clazz
     * @return
     */
    public static <T> List<T> toList(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz);
    }
    /**
     * 将string转化为序列化的json字符串
     * @return
     */
    public static Object textToJson(String text) {
        Object objectJson = JSON.parse(text);
        return objectJson;
    }
    /**
     * json字符串转化为map
     *
     * @param s
     * @return
     */
    public static <K, V> Map<K, V> stringToCollect(String s) {
        Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
        return m;
    }

    /**
     * 转换JSON字符串为对象
     *
     * @param jsonData
     * @param clazz
     * @return
     */
    public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
        return JSONObject.parseObject(jsonData, clazz);
    }

    /**
     * 将map转化为string
     *
     * @param m
     * @return
     */
    public static <K, V> String collectToString(Map<K, V> m) {
        String s = JSONObject.toJSONString(m);
        return s;
    }

}
